<template>
  <div class="container">
    <div class="package-card bg-white hd-shadow">

      <van-row>
        <van-search
                v-model="value"
                placeholder="请输入城市名称"
                show-action
                @search="onSearch"
        >
          <div slot="action" @click="onSearch">搜索</div>
        </van-search>

      </van-row>
      <van-cell-group>
        <van-cell
                v-for="(item, index) in newlist" :key="index"
                :title="item.name"
                icon="location-o"
                @click="info(item)"
        />
      </van-cell-group>
    </div>
  </div>
</template>

<script>
export default {
  name: 'line_query',
  data () {
    return {
      value: '',
      newlist: []
    }
  },
  methods: {
    init () {
      this.$http({
        fun: 'transitcity',
        requestType: 'get',
        cb: res => {
          console.log(res)
          this.newlist = res
          return this.newlist.sort(function (x, y) {
            return x['name'].localeCompare(y['name'])
          })
        }
      })
    },
    info (item) {
      console.log(item.id)
      this.$router.push({
        name: 'bus_query',
        query: {
          'cityid': item.cityid,
          'name': item.name
        }
      })
    },
    onSearch () {
      console.log('search')
      for (var i = 0; i <= this.newlist.length; i++) {
        if (this.value === this.newlist[i].name) {
          console.log(this.newlist[i].name)
          this.newlist = [{
            name: this.newlist[i].name,
            cityid: this.newlist[i].cityid,
            code: this.newlist[i].code
          }]
          console.log(this.newlist)
          this.newlist.forEach(item => {
            console.log('111')
          })
        }
      }
    }
  },

  mounted () {
    this.init()
  }
}
</script>

<style src="./index.css" scoped>
</style>
